<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2021/6/9
 * Time: 14:51
 * 私人订制客户
 */

namespace app\api\controller;


use think\Db;

class Privateclient extends Base
{
    public $user_id;
    public $create_time;
    public $page;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status' => 301, 'msg' => $this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 我的客户列表
     * @return \think\response\Json
     * page     页码
     */
    public function clientList(){
        $show = input('show_type'); // 1 我的客户    2 审核、工程师、仓管客户列表
        $keyword = input('keyword');
        $where['pc_client_status'] = 1;
        if($show == 1){
            $where['pc_user_id'] = $this->user_id;
        }
        if(isset($keyword) && $keyword != ''){
            $where['pc_client_name'] = ['LIKE', '%' . $keyword . '%'];
        }
        $list = Db::table('up_private_client')
            ->where($where)
            ->field('pc_id, pc_client_name, pc_client_phone, pc_client_age, pc_client_gender,
            pc_client_marry, pc_client_remark, pc_client_default')
            ->order('pc_client_default asc, pc_client_time desc')
            ->page($this->page, 10)
            ->select();
        if($list){
            // 展示该客户订单数量
            if($show == 1){
                $order = Db::table('up_private_order')
                    ->where('po_cate', 1)
                    ->field('po_client_id')
                    ->select();
                foreach ($list as $k => $v){
                    $order_number = 0;
                    if($order){
                        foreach ($order as $vo){
                            if($v['pc_id'] == $vo['po_client_id']){
                                $order_number ++;
                            }
                        }
                    }
                    $list[$k]['order_number'] = $order_number;
                }
            }
            return json(['status' => 200, 'msg' => $list]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 新增客户
     * @return \think\response\Json
     * pc_client_name    客户姓名
     * pc_client_phone   客户手机号
     * pc_client_age     客户年龄
     * pc_client_gender  客户性别   1 男   2 女
     * pc_client_marry   客户婚姻状态   1 未婚   2 已婚
     * pc_client_remark  备注
     */
    public function addClient(){
        $pc_client_name = input('pc_client_name');
        $pc_client_phone = input('pc_client_phone');
        $pc_client_age = input('pc_client_age');
        $pc_client_gender = input('pc_client_gender'); // 1 男   2 女
        $pc_client_marry  = input('pc_client_marry'); // 1 未婚   2 已婚
        $pc_client_remark = input('pc_client_remark');
        
        $client = [
            'pc_user_id' => $this->user_id,
            'pc_client_name' => $pc_client_name,
            'pc_client_phone' => $pc_client_phone,
            'pc_client_age' => $pc_client_age,
            'pc_client_gender' => $pc_client_gender,
            'pc_client_marry' => $pc_client_marry,
            'pc_client_remark' => $pc_client_remark,
            'pc_client_time' => $this->create_time
        ];
        $pc_id = Db::table('up_private_client')->insertGetId($client);
        if($pc_id){
            return json(['status' => 200, 'msg' => '', 'pc_id' => $pc_id]);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
        }
    }
    
    /**
     * 客户信息回显
     * @return \think\response\Json
     * pc_id      客户ID
     */
    public function showClient(){
        $pc_id = input('pc_id');
        $client = Db::table('up_private_client')
            ->where('pc_id', $pc_id)
            ->field('pc_id, pc_client_name, pc_client_phone, pc_client_age, pc_client_gender, pc_client_marry, 
            pc_client_remark')
            ->find();
        return json(['status' => 200, 'msg' => $client]);
    }
    
    /**
     * 编辑客户
     * @return \think\response\Json
     * pc_client_name    客户姓名
     * pc_client_phone   客户手机号
     * pc_client_age     客户年龄
     * pc_client_gender  客户性别   1 男   2 女
     * pc_client_marry   客户婚姻状态   1 未婚   2 已婚
     * pc_client_remark  备注
     * pc_id             客户ID
     */
    public function updateClient(){
        $pc_id = input('pc_id');
        $pc_client_name = input('pc_client_name');
        $pc_client_phone = input('pc_client_phone');
        $pc_client_age = input('pc_client_age');
        $pc_client_gender = input('pc_client_gender'); // 1 男   2 女
        $pc_client_marry  = input('pc_client_marry'); // 1 未婚   2 已婚
        $pc_client_remark = input('pc_client_remark');
    
        $client = [
            'pc_client_name' => $pc_client_name,
            'pc_client_phone' => $pc_client_phone,
            'pc_client_age' => $pc_client_age,
            'pc_client_gender' => $pc_client_gender,
            'pc_client_marry' => $pc_client_marry,
            'pc_client_remark' => $pc_client_remark,
            'pc_client_update_time' => $this->create_time
        ];
        $res = Db::table('up_private_client')->where('pc_id', $pc_id)->update($client);
        if($res){
            return json(['status' => 200, 'msg' => '', 'pc_id' => $pc_id]);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
        }
    }
    
    /**
     * 删除客户
     * @return \think\response\Json
     * pc_id      客户ID
     */
    public function delClient(){
        $pc_id = input('pc_id');
        $res = Db::table('up_private_client')->where('pc_id', $pc_id)->setField('pc_client_status', 2);
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
        }
    }

    /**
     * 设置默认
     * pc_id    客户ID
     * @return \think\response\Json
     */
    public function setDefault(){
        $pc_id = input('pc_id');

        Db::startTrans();
        try{
            Db::table('up_private_client')
                ->where('pc_user_id', $this->user_id)
                ->where('pc_client_default', 1)
                ->setField('pc_client_default', 2);

            Db::table('up_private_client')->where('pc_id', $pc_id)->setField('pc_client_default', 1);

            Db::commit();
            return json(['status' => 200, 'msg' => '操作成功']);
        }catch (\Exception $e){
            Db::rollback();
            return json(['status' => 0, 'msg' => $e->getMessage()]);
        }
    }

}